/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mb.webserver.handler;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mb.mbmiddle.MbPartMiddle;
import mb.webserver.inMemory.NewsDataProcess;
import mb.webserver.inMemory.PromotionDataProcess;
import mb.webserver.inMemory.ServiceDetailProcess;

/**
 *
 * @author thanh.nguyenchi
 */
public class NewsData extends BaseServlet{   
    
    public NewsData(){
        super();        
    }
    
    @Override
    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException
    {         
    ///api/loadServieDetail?version={1}&lang={2}&serviceId=20&callback=
        String sOut = "";       
        String version          = request.getParameter("version");              // check 
        String lang             = request.getParameter("lang");                 // log
        String cateId           = request.getParameter("cateid");               // log
        String page             = request.getParameter("page");                 // log
        String callback         = request.getParameter("callback");             // check 
        
        StringBuffer sbLog = new StringBuffer();
        sbLog.append("NewsData ==> [v:" + version);
        sbLog.append("] [i:" + lang + "] sId:[" + cateId);
        sbLog.append("] [p:" + page + "]");
        sbLog.append("] [c:" + callback + "] from [ip:" + request.getRemoteAddr() + "]");
        log(sbLog.toString());
        /*
        {\"status\":0,\"error\":\"\",\"news\":[{\"News_ID\":27,\"Title\":\"Gi\u1EA3m ngay 20% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/food.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6},{\"News_ID\":31,\"Title\":\"Gi\u1EA3m ngay 20% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/1.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6},{\"News_ID\":32,\"Title\":\"Gi\u1EA3m ngay 25% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/2.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6},{\"News_ID\":33,\"Title\":\"Gi\u1EA3m ngay 30% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/3.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6},{\"News_ID\":35,\"Title\":\"Gi\u1EA3m ngay 40% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/5.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6},{\"News_ID\":36,\"Title\":\"Gi\u1EA3m ngay 45% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/6.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6},{\"News_ID\":37,\"Title\":\"Gi\u1EA3m ngay 50% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/7.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6},{\"News_ID\":38,\"Title\":\"Gi\u1EA3m ngay 55% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/1.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6},{\"News_ID\":39,\"Title\":\"Gi\u1EA3m ngay 60% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/2.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6},{\"News_ID\":40,\"Title\":\"Gi\u1EA3m ngay 65% cho c\u00E1c lo\u1EA1i pizza trong th\u1EF1c \u0111\u01A1n H\u1EA3i S\u1EA3n c\u1EE7a Pizza Hut c\u00F9ng MB Visa\",\"Address\":\"S\u1ED1 18 Tam Trinh, Qu\u1EADn Hai B\u00E0 Tr\u01B0ng, H\u00E0 N\u1ED9i.\",\"Teaser\":\"Mo ta chuong trinh\",\"Content\":\"K\u1EC3 t\u1EEB nay \u0111\u1EBFn h\u1EBFt ng\u00E0y 31\/8\/2013, MB d\u00E0nh \u01B0u \u0111\u00E3i \u0111\u1EB7c bi\u1EC7t cho ch\u1EE7 th\u1EBB MB Visa t\u1EA1i c\u00E1c c\u1EEDa h\u00E0ng Pizza Hut. <\\\/div>\",\"Avatar\":\"http:\/\/202.37.87.37:8080\/img\/3.png\",\"Latitude\":21.02594,\"Longitude\":105.85516,\"FromDate\":\"2013-05-05\",\"DueDate\":\"2014-05-05\",\"Phone\":\"\",\"OrigLink\":\"\",\"Cate_ID\":6}],\"totalCount\":27}
         // Check cate id in service
        boolean hasService = hasService(cateId);
        boolean hasService = hasService(cateId);
        if (hasService == true){
            // call bussiness
            ServiceDetailProcess objProcess = new ServiceDetailProcess(lang,cateId);
            if ((callback != null) && (callback.length() > 1)){
                sOut = callback + "(" + objProcess.getResult() + ")";
            }
            else{
                // return no callback
                sOut = objProcess.getResult();
            }
        }
        else{
            // Check cate id
            if (cateId.equals(MbPartMiddle.objDbConfig.getPromotion_string())){
                // promotion
                // call bussiness
                PromotionDataProcess objProcess = new PromotionDataProcess(lang,"");
                if ((callback != null) && (callback.length() > 1)){
                    sOut = objProcess.getResult();
                }
                else{
                    // return no callback
                    sOut = objProcess.getResult();
                }
            }
            else{
                // call bussiness
                NewsDataProcess objProcess = new NewsDataProcess(lang,cateId,page);
                if ((callback != null) && (callback.length() > 1)){
                    sOut = objProcess.getResult();
                }
                else{
                    // return no callback
                    sOut = objProcess.getResult();
                }
            }
        }
         */
                    
        sOut="[{\"Title\":\"Giảm ngay 20% cho các loại pizza trong thực đơn Hải Sản của Pizza Hut cùng MB Visa\"},{\"Title\":\"Giảm ngay 100%\"}]";
        response.setContentType("application/json; charset=UTF-8"); 
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println(sOut);
    }
    
    private boolean hasService(String cateId){
        try{
            boolean flag = MbPartMiddle.objDbConfig.checkService(new Long(cateId));
            return flag;
        }
        catch (Exception ex){
            return false;
        }
    }
}
